package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type TierCommissionReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 层级返佣报表
func NewTierCommissionReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TierCommissionReportLogic {
	return &TierCommissionReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TierCommissionReportLogic) TierCommissionReport(req *types.TierCommissionReportReq) (resp *types.TierCommissionReportResp, err error) {
	report, err := l.svcCtx.AgentRPC.GetTierAgentReport(l.ctx, &rpc.GetTierAgentReportReq{
		Date: req.Day,
	})
	if err != nil {
		return nil, err
	}

	data := make([]types.TierCommissionReportOne, 0)
	for _, v := range report.Data {
		gameCommission := make([]types.GameCommissionOne, 0)
		for _, v2 := range v.List {
			gameCommission = append(gameCommission, types.GameCommissionOne{
				CategoryCode:     v2.CategoryCode,
				CategoryName:     v2.CategoryName,
				GameAmount:       v2.GameAmount,
				CommissionAmount: v2.CommissionAmount,
				LevelFeeRatio:    v2.LevelFee,
			})
		}
		one := types.TierCommissionReportOne{
			Tier:             v.Tier,
			RechargeCount:    v.RechargeCount,
			RechargeAmount:   v.RechargeAmount,
			CommissionsTotal: v.Commissions,
			GameCommission:   gameCommission,
		}
		data = append(data, one)
	}

	resp = &types.TierCommissionReportResp{
		Data: data,
	}
	return
}
